package com.atguigu.guli.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {
    
    @Bean
    public CorsWebFilter corsWebFilter(){
        //基于路径的方式配置跨域参数：
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://manager.gmall.com");//网关项目(api.gmall.com)允许 manager.gmall.com的页面发起异步请求
        config.addAllowedOrigin("http://www.gmall.com");//网关项目(api.gmall.com)允许 manager.gmall.com的页面发起异步请求
        config.addAllowedMethod("*");//允许所有的请求方式跨域访问
        config.addAllowedHeader("*");//允许携带任意请求头跨域访问
        config.setAllowCredentials(true);//允许携带cookie跨域访问
        configSource.registerCorsConfiguration("/**",config);//给某个路径配置允许跨域的参数
        return new CorsWebFilter(configSource);
    }
    
}
